package com.ilancuo.file;

import java.io.*;

/**
 * 递归输出文件目录中的所有文件
 * Created by ilancuo on 2018/4/9.
 */
public class FileList {

    /**
     * 列出某个文件夹下的文件列表
     */
    private static void listFiles(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files == null) return;
            for (File f : files) {
                if (f.isDirectory()) {
                    listFiles(f);
                } else {
                    System.out.println(f.getAbsolutePath());
                }
            }
        }
    }

    /**
     * 逐行读取文件
     */
    public static String readFileByLine(String fileName) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        try {
            br = new BufferedReader(new FileReader(fileName));
            String temp;
            while ((temp = br.readLine()) != null) {
                sb.append(new String(temp.getBytes(), "UTF-8").trim()).append("\n");
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(readFileByLine("/Users/ilancuo/NutCloud/hosts"));
    }
}
